package pda;

import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

/**
 * @author haoly
 * @title Stack
 * @date 2025/4/21 09:35
 * @description TODO
 */
public class Stack <T>{

    private LinkedList<T> contents;

    public Stack() {
        this.contents = new LinkedList<>();
    }

    public Stack(List<T> contents) {
        this.contents = new LinkedList<>();
        if (contents != null) {
            this.contents.addAll(contents);
        }

    }

    public void push(T data){
        this.contents.addFirst(data);
    }

    public T pop(){
        return this.contents.removeFirst();
    }

    public T top(){
        return this.contents.getFirst();
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("#<Stack (");
        sb.append(top()).append(")");
        for (int i = 1; i < contents.size(); i++) {
            sb.append(contents.get(i));
        }
        sb.append(">");
        return sb.toString();
    }

    public Stack<T> copy() {
        return new Stack<T>(contents);
    }

    @Override
    public int hashCode() {
        return Objects.hash(contents);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null || !(obj instanceof Stack<?>)) {
            return false;
        }
        Stack<T> otherStack = (Stack<T>) obj;

        return Objects.equals(contents, otherStack.contents);
    }
}
